// pages/discuss/discuss.js
import { postList } from "../../API/api"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    leftList: [],
    rightList: [],
    leftHight: 0,
    rightHight: 0,
    pagenum: 1,
    list: [],
    finish: false,
    flag: false
  },


  postinit(num) {
    postList(15, num).then(res => {
      // console.log(res);
      let item = res.data.rows
      // this.data.list = this.data.list.concat(item)
      let leftlist = this.data.leftList
      let rightlist = this.data.rightList
      let leftheight = this.data.leftHight
      let rightheight = this.data.rightHight
      let that = this
      // console.log(item);
      if (this.data.pagenum * 15 >= res.data.total) {
        // 数据已经加载完了，不需要在加载数据了
        this.setData({
          flag: true
        })
      }
      for (let i = 0; i < item.length; i++) {
        wx.getImageInfo({
          src: item[i].coverImgUrl,
          success(res) {
            // console.log(res);
            if (leftheight <= rightheight) {
              leftlist.push(item[i])
              leftheight += res.height / res.width
              that.setData({
                leftList: leftlist,
                leftHight: leftheight
                // rightList: rightlist,
              })
            } else {
              rightlist.push(item[i])
              rightheight += res.height / res.width
              that.setData({
                // leftList: leftlist,
                rightList: rightlist,
                rightHight: rightheight
              })
            }
          }
        })
      }
      this.setData({
        finish: true
      })
    })
  },

  tochat() {
    wx.navigateTo({
      url: '../chat/chat',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.postinit(this.data.pagenum)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // this.postinit(this.data.pagenum)
    if (this.data.finish == true) {
      wx.stopPullDownRefresh({
        success(res) {
          wx.showToast({
            title: "刷新成功",
          })
        }
      })
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.flag == true) {
      return
    } else {
      this.postinit(++(this.data.pagenum))
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})